package com.qc31.monitorlib.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider
import com.amap.api.maps.AMap
import com.amap.api.maps.MapsInitializer
import com.qc31.baselib.custom.MLoadingDialog
import com.qc31.baselib.entity.ToastEntity
import com.qc31.baselib.utils.toast
import com.qc31.monitorlib.databinding.IncludeMapMatchBinding

/**
 *@Author Jasper
 *@Time   2020/8/1
 *@Desc   地图基础Fragment
 */
abstract class BaseMapFragment<VB : ViewBinding>(
    private val inflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {

    private lateinit var mapBindingVB: IncludeMapMatchBinding
    private var isFirstLoad = true // 是否第一次加载
    lateinit var binding: VB
    protected var mMap: AMap? = null

    protected val scopeProvider: AndroidLifecycleScopeProvider by lazy {
        AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = inflater(layoutInflater, container, false)
        mapBindingVB = IncludeMapMatchBinding.bind(binding.root)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        MapsInitializer.updatePrivacyShow(requireContext(), true, true)
        MapsInitializer.updatePrivacyAgree(requireContext(), true)
        mapBindingVB.mMapView.onCreate(savedInstanceState)
        mMap = mapBindingVB.mMapView.map
        initView(view)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isFirstLoad = true
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            isFirstLoad = false
            // 将数据加载逻辑放到onResume()方法中
            initData()
            initEvent()

        }
        mapBindingVB.mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapBindingVB.mMapView.onPause()
    }

    override fun onDestroy() {
        mMap?.clear()
        mMap = null
        mapBindingVB.mMapView.onDestroy()
        MLoadingDialog.dismiss()
        super.onDestroy()

    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapBindingVB.mMapView.onSaveInstanceState(outState)
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    abstract fun initView(view: View)

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 初始化事件
     */
    abstract fun initEvent()

    protected fun isShowLoading(isShow: Boolean) {
        if (isShow) MLoadingDialog.showLoading(requireActivity()) else MLoadingDialog.dismiss()
    }

    protected fun showToast(messageId: Int) {
        showToast(getString(messageId))
    }

    protected fun showToast(message: String) {
        requireActivity().toast(message)
    }

    fun toastObserver(entity: ToastEntity) {
        if (entity.toast.isNotEmpty()) {
            requireActivity().toast(entity.toast)
        }
        if (entity.toastId != 0) {
            requireActivity().toast(entity.toastId)
        }
        isShowLoading(entity.isShowLoading)
    }
}